package com.ruoyi.base.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjUtil;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.base.domain.bo.BaseStandardTenderSheetBo;
import com.ruoyi.base.domain.vo.BaseStandardTenderSheetVo;
import com.ruoyi.base.domain.BaseStandardTenderSheet;
import com.ruoyi.base.mapper.BaseStandardTenderSheetMapper;
import com.ruoyi.base.service.IBaseStandardTenderSheetService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 标准清单Service业务层处理
 *
 * @author zyb
 * @date 2024-09-24
 */
@RequiredArgsConstructor
@Service
public class BaseStandardTenderSheetServiceImpl implements IBaseStandardTenderSheetService {

    private final BaseStandardTenderSheetMapper baseMapper;

    /**
     * 查询标准清单
     */
    @Override
    public BaseStandardTenderSheetVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询标准清单列表
     */
    @Override
    public TableDataInfo<BaseStandardTenderSheetVo> queryPageList(BaseStandardTenderSheetBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BaseStandardTenderSheet> lqw = buildQueryWrapper(bo);
        Page<BaseStandardTenderSheetVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询标准清单列表
     */
    @Override
    public List<BaseStandardTenderSheetVo> queryList(BaseStandardTenderSheetBo bo) {
        LambdaQueryWrapper<BaseStandardTenderSheet> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<BaseStandardTenderSheet> buildQueryWrapper(BaseStandardTenderSheetBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BaseStandardTenderSheet> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getSheetTypeCode()), BaseStandardTenderSheet::getSheetTypeCode, bo.getSheetTypeCode());
        lqw.like(StringUtils.isNotBlank(bo.getSheetTypeName()), BaseStandardTenderSheet::getSheetTypeName, bo.getSheetTypeName());
        lqw.eq(StringUtils.isNotBlank(bo.getSheetFeaturesCode()), BaseStandardTenderSheet::getSheetFeaturesCode, bo.getSheetFeaturesCode());
        lqw.like(StringUtils.isNotBlank(bo.getSheetFeaturesName()), BaseStandardTenderSheet::getSheetFeaturesName, bo.getSheetFeaturesName());
        lqw.eq(StringUtils.isNotBlank(bo.getSheetUnit()), BaseStandardTenderSheet::getSheetUnit, bo.getSheetUnit());
        lqw.eq(bo.getSheetSuggestUnitPrice() != null, BaseStandardTenderSheet::getSheetSuggestUnitPrice, bo.getSheetSuggestUnitPrice());
        lqw.eq(bo.getSheetTaxRate() != null, BaseStandardTenderSheet::getSheetTaxRate, bo.getSheetTaxRate());
        lqw.eq(bo.getUnitPriceWithRate() != null, BaseStandardTenderSheet::getUnitPriceWithRate, bo.getUnitPriceWithRate());
        lqw.orderByAsc(BaseStandardTenderSheet::getSheetTypeCode);
        return lqw;
    }

    /**
     * 新增标准清单
     */
    @Override
    public Boolean insertByBo(BaseStandardTenderSheetBo bo) {
        // 系统分配 唯一 清单编码
        bo.setSheetFeaturesCode(bo.getSheetTypeCode()+ IdUtil.getSnowflakeNextIdStr());
        BaseStandardTenderSheet add = BeanUtil.toBean(bo, BaseStandardTenderSheet.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改标准清单
     */
    @Override
    public Boolean updateByBo(BaseStandardTenderSheetBo bo) {
        BaseStandardTenderSheet update = BeanUtil.toBean(bo, BaseStandardTenderSheet.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(BaseStandardTenderSheet entity){
        // 如果 同一类型、同一名称、同一特征 已经存在，则不允许添加
        if (baseMapper.exists(new LambdaQueryWrapper<BaseStandardTenderSheet>()
            .ne(ObjUtil.isNotNull(entity.getId()), BaseStandardTenderSheet::getId, entity.getId())
            .eq(BaseStandardTenderSheet::getSheetTypeCode, entity.getSheetTypeCode())
            .eq(BaseStandardTenderSheet::getSheetTypeName, entity.getSheetTypeName())
            .eq(BaseStandardTenderSheet::getSheetFeaturesName, entity.getSheetFeaturesName()))) {
            throw new ServiceException("系统中已存在 此清单类型编码、清单类型名称、规格型号编码、规格型号名称的标准清单");
        }
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除标准清单
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
